package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageResult;
import com.woniu.mapper.GoodsMapper;
import com.woniu.mapper.ImgsMapper;
import com.woniu.mapper.SortMapper;
import com.woniu.pojo.Goods;
import com.woniu.pojo.Imgs;
import com.woniu.pojo.Sort;
import com.woniu.service.ISortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanMax
 * @since 2022-10-12
 */
@Service
public class SortServiceImpl extends ServiceImpl<SortMapper, Sort> implements ISortService {
    @Autowired
    private SortMapper sortMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ImgsMapper imgsMapper;

    @Override
    public PageResult search(Sort sort, Integer currentPage, Integer pageSize) {
        Page<Sort> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Sort> wrapper = new QueryWrapper<>();
        if (sort!=null){
            if(!StringUtils.isEmpty(sort.getTitle())){
                wrapper.eq("title",sort.getTitle());
            }
            if (!StringUtils.isEmpty(sort.getNumber())){
                wrapper.eq("number",sort.getNumber());
            }
        }
        Page<Sort> sortPage = sortMapper.selectPage(page, wrapper);
        return new PageResult(sortPage.getTotal(),sortPage.getRecords());
    }

    @Override
    public Integer deleteByIdAll(String id) {
        int i = sortMapper.deleteById(id);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("sort_id",id);
        List<Goods> goods = goodsMapper.selectList(wrapper);
        System.out.println("=========="+goods);
        for (Goods good : goods) {
            QueryWrapper<Imgs> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("goods_id",good.getId());
            imgsMapper.delete(wrapper1);
        }
        goodsMapper.delete(wrapper);
        return i;
    }
}
